#include "DebugGrid.h"
#define ZLEVEL 0.0

DebugGrid::DebugGrid()
	:g_pVB(NULL)
{
	scale=100.0;
	m_gridEnd=10;
	color=D3DCOLOR_ARGB(0,0,0,0);
}

DebugGrid::~DebugGrid()
{
	if(g_Vertices != NULL)
	{
		delete g_Vertices;
		g_Vertices = NULL;
	}
}

void DebugGrid::createGrid(LPDIRECT3DDEVICE9 device)
{
	//HRESULT hr;
	g_Vertices = new CUSTOMVERTEX[8*m_gridEnd];
	//creates the vertices
	int count=0;
	for(int i=0;i<=m_gridEnd;i++)
	{
		//line1
		if(i != m_gridEnd)
		{
			g_Vertices[count].x=((float)m_gridEnd*scale)-(i*scale);
			g_Vertices[count].z=scale*(float)m_gridEnd;
			g_Vertices[count].y=ZLEVEL;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=color;
			count++;
			g_Vertices[count].x=((float)m_gridEnd*scale)-(i*scale);
			g_Vertices[count].z=(-1*scale)*(float)m_gridEnd;
			g_Vertices[count].y=ZLEVEL;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=color;
			count++;
		}
		else
		{
			g_Vertices[count].x=0.0;
			g_Vertices[count].z=0.0;
			g_Vertices[count].y=scale*(float)m_gridEnd;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=D3DCOLOR_ARGB(0,255,0,0);
			count++;
			g_Vertices[count].x=0.0;
			g_Vertices[count].z=0.0;
			g_Vertices[count].y=(-1*scale)*(float)m_gridEnd;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=D3DCOLOR_ARGB(0,255,0,0);
			count++;
		}
		//line2
			g_Vertices[count].x=-1*(((float)m_gridEnd*scale)-(i*scale));
			g_Vertices[count].z=scale*(float)m_gridEnd;
			g_Vertices[count].y=ZLEVEL;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=color;
			count++;
			g_Vertices[count].x=-1*(((float)m_gridEnd*scale)-(i*scale));
			g_Vertices[count].z=(-1*scale)*(float)m_gridEnd;
			g_Vertices[count].y=ZLEVEL;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=color;
			count++;
		//line3
		if(i != m_gridEnd)
		{
			g_Vertices[count].z=((float)m_gridEnd*scale)-(i*scale);
			g_Vertices[count].x=scale*(float)m_gridEnd;
			g_Vertices[count].y=ZLEVEL;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=color;
			count++;
			g_Vertices[count].z=((float)m_gridEnd*scale)-(i*scale);
			g_Vertices[count].x=(-1*scale)*(float)m_gridEnd;
			g_Vertices[count].y=ZLEVEL;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=color;
			count++;
		}
		else
		{
			g_Vertices[count].z=scale*(float)m_gridEnd;
			g_Vertices[count].x=scale*(float)m_gridEnd;
			g_Vertices[count].y=ZLEVEL;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=D3DCOLOR_ARGB(0,255,0,255);
			count++;
			g_Vertices[count].z=(-1*scale)*(float)m_gridEnd;
			g_Vertices[count].x=(-1*scale)*(float)m_gridEnd;
			g_Vertices[count].y=ZLEVEL;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=D3DCOLOR_ARGB(0,255,0,255);
			count++;

		}
		//line4
			g_Vertices[count].z=-1*(((float)m_gridEnd*scale)-(i*scale));
			g_Vertices[count].x=scale*(float)m_gridEnd;
			g_Vertices[count].y=ZLEVEL;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=color;
			count++;
			g_Vertices[count].z=-1*(((float)m_gridEnd*scale)-(i*scale));
			g_Vertices[count].x=(-1*scale)*(float)m_gridEnd;
			g_Vertices[count].y=ZLEVEL;
			g_Vertices[count].rhw=1.0;
			g_Vertices[count].color=color;
			count++;
	}
}

void DebugGrid::drawGrid(LPDIRECT3DDEVICE9 device)
{
	HRESULT hr;
	dxMgr.MatrixStack->Push();
	device->SetTransform(D3DTS_WORLD, dxMgr.MatrixStack->GetTop());
	hr=device->DrawPrimitiveUP(D3DPT_LINELIST,4*m_gridEnd+4,g_Vertices,sizeof(CUSTOMVERTEX));
	dxMgr.MatrixStack->Pop();
}

void DebugGrid::shutdowngrid()
{
	if(g_Vertices != NULL)
	{
		delete g_Vertices;
		g_Vertices = NULL;
	}
}